package com.music.note_android.ui.folder

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.music.note_android.R
import com.music.note_android.data.model.Folder

class FolderSelectionAdapter(
    private val onFolderSelected: (Folder) -> Unit
) : ListAdapter<Folder, FolderSelectionAdapter.FolderSelectionViewHolder>(FolderDiffCallback()) {

    private var selectedFolderId: String? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FolderSelectionViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_folder_selection, parent, false)
        return FolderSelectionViewHolder(view)
    }

    override fun onBindViewHolder(holder: FolderSelectionViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class FolderSelectionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val radioButton: RadioButton = itemView.findViewById(R.id.rb_folder_selection)
        private val folderIcon: ImageView = itemView.findViewById(R.id.iv_folder_icon)
        private val folderName: TextView = itemView.findViewById(R.id.tv_folder_name)
        private val noteCount: TextView = itemView.findViewById(R.id.tv_note_count)
        private val systemFolderBadge: TextView = itemView.findViewById(R.id.tv_system_folder_badge)

        fun bind(folder: Folder) {
            // 设置文件夹名称
            folderName.text = folder.name
            
            // 设置笔记数量
            noteCount.text = folder.getNoteCountText()
            
            // 设置选择状态
            radioButton.isChecked = selectedFolderId == folder.id
            
            // 设置系统文件夹标识和图标
            if (folder.isSystem) {
                systemFolderBadge.visibility = View.VISIBLE
                folderIcon.setImageResource(R.drawable.ic_folder_24)
            } else {
                systemFolderBadge.visibility = View.GONE
                folderIcon.setImageResource(R.drawable.ic_folder_24)
            }
            
            // 设置点击事件
            itemView.setOnClickListener {
                selectFolder(folder)
            }
            
            radioButton.setOnClickListener {
                selectFolder(folder)
            }
        }
        
        private fun selectFolder(folder: Folder) {
            val previousSelectedId = selectedFolderId
            selectedFolderId = folder.id
            
            // 更新之前选中项的状态
            if (previousSelectedId != null) {
                notifyItemChanged(currentList.indexOfFirst { it.id == previousSelectedId })
            }
            
            // 更新当前选中项的状态
            notifyItemChanged(adapterPosition)
            
            // 回调选择事件
            onFolderSelected(folder)
        }
    }

    class FolderDiffCallback : DiffUtil.ItemCallback<Folder>() {
        override fun areItemsTheSame(oldItem: Folder, newItem: Folder): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Folder, newItem: Folder): Boolean {
            return oldItem == newItem
        }
    }
}